package com.sc.controller.patrol;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.domain.patrol.PatrolProject;
import com.sc.dto.PatrolProjectDTO;
import com.sc.result.Result;
import com.sc.service.patrol.PatrolProjectService;
import com.sc.vo.PatrolProjectVO;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/*
 * 巡更项目管理
 * @author:Zuiliu
 * 功能：增删改查、作列表展示
 * 请求参数
 {
  "add_person": "测试人员A",
  "add_time": "2024-11-25 15:36:33",
  "project_name": "水表巡检",
  "project_rule": "标准B",
  "project_stable": "未开始"
}
 * */
@CrossOrigin
@RestController
@RequestMapping("/patrolProject")
@RequiredArgsConstructor
public class PatrolProjectController {

    private final PatrolProjectService ivrs;

    // 添加巡更项目
    @PostMapping("/addPatrolProject")
    public Result addPatrolProject(@RequestBody PatrolProject patrolProject) {
        patrolProject.setAddTime(LocalDateTime.now());
        try {
            return Result.success("添加成功", ivrs.save(patrolProject));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更项目名称重复，换一个试试");
        }
    }

    // 删除巡更项目
    @DeleteMapping("/deletePatrolProject")
    public Result deletePatrolProject(Integer id) {
        return Result.success("删除成功", ivrs.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", ivrs.removeByIds(ids));
    }

    // 修改巡更项目
    @PutMapping("/updatePatrolProject")
    public Result updatePatrolProject(@RequestBody PatrolProject patrolProject) {
        try {
            return Result.success("修改成功", ivrs.updateById(patrolProject));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更项目名称重复，换一个试试");
        }
    }

    // 查询巡更项目
    @GetMapping("/selectPatrolProject")
    public Result<PatrolProject> selectPatrolProject(Integer id) {
        return Result.success(ivrs.getById(id));
    }

    // 列表展示
    @PostMapping("/listPatrolProject")
    public Result<Page<PatrolProjectVO>> listPatrolProject(@RequestBody PatrolProjectDTO patrolProjectDTO) {
        return Result.success(ivrs.pageQuery(patrolProjectDTO));
    }

    @GetMapping("/getAll")
    public Result<List<PatrolProject>> getAll() {
        return Result.success(ivrs.list());
    }
}
